import { ref } from 'vue';
import { getInboundOrdersDetailsApi, deletePurchaseRequestFormApi, updateDeliveryFlagApi } from '@/api/sj/purchaseManagement/toBeStored/index'

const usePurchaseReceipt = () => {
    const { proxy } = getCurrentInstance() as any;

    const purchaseVisible = ref(false);
    const purchaseDetails = ref();
    const purchaseRow = ref();

    /**
     * @description: 打开采购入库的弹窗
     */
    const handleOpenPurchase = async (row: any) => {
        console.log(row);

        if (row.inStatus == '2') return proxy?.$modal.msgError('当前采购单已经入库完成');

        const data = await handleGetPurchaseDetails(row)
        console.log(data, 'code, 123123');

        purchaseRow.value = row;
        purchaseDetails.value = data

        purchaseVisible.value = true;
    };

    /**
     * @description 采购申请单 采购入库
     */
    const handlePurchase = (row: any) => {
        // ElMessageBox.confirm('无法入库，可能的原因：1.已对账2.已全部入库3.未市批', '提示', {
        //     confirmButtonText: '确定',
        //     cancelButtonText: '取消',
        //     type: 'warning'
        // })
        //     .then(() => {
        //     })
        //     .catch(() => { });


        purchaseRow.value = row;
        purchaseVisible.value = true;
    };



    /**
     * 获取采购入库详情
     */
    const handleGetPurchaseDetails = (row: any) => {
        return new Promise((resolve, reject) => {
            getInboundOrdersDetailsApi(row.id).then((res: any) => {
                resolve(res.data);
            }).catch((err: any) => {
                reject(err);
            });
        });
    };

    /**
     * @description: 删除入库信息
     */
    const handleDelete = (row: any, fn: Function) => {
        if (row.deliveryFlag == '1') {
            return proxy?.$modal.msgError('当前采购单已经标识, 无法删除!');
        } else {
            ElMessageBox.confirm('此条待入库信总将永久删除，请问您确认要这么做么？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {
                    try {
                        const { code } = await deletePurchaseRequestFormApi(row.id);
                        if (code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '删除成功!'
                            });

                            fn();
                        }
                    } catch (error) {

                    }
                })
                .catch(() => { });
        }

    };

    /**
     * @description: 发货标识 
     */
    const handleDeliverGoods = (row: any, fn: Function) => {
        if (row.deliveryFlag == '0') {
            ElMessageBox.confirm('点击后此采陶单将转化为已发货状态，转化后不得除。您确定么？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(async () => {

                    try {
                        const { code } = await updateDeliveryFlagApi({ id: row.id })
                        if (code == 200) {
                            ElMessage({
                                type: 'success',
                                message: '操作成功!'
                            });

                            fn();
                        }
                    } catch (error) {

                    }
                })
                .catch(() => { });
        } else {
            return proxy?.$modal.msgError('当前采购单已经标识');
        }
    };



    return {
        purchaseVisible,
        purchaseDetails,
        purchaseRow,
        handleDelete,
        handlePurchase,
        handleDeliverGoods,
        handleOpenPurchase
    }
};

export default usePurchaseReceipt;